package com.hrpmar.controller;

import com.alibaba.fastjson.JSON;
import com.hrpmar.controller.dto.MedicperDto;
import com.hrpmar.pojo.Division;
import com.hrpmar.pojo.Medicper;
import com.hrpmar.pojo.Role;
import com.hrpmar.pojo.Worker;
import com.hrpmar.service.MedicperService;
import com.hrpmar.utils.Constants;
import com.hrpmar.utils.Pager;
import com.hrpmar.utils.SystemContext;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by 李松松 on 2017/11/15.
 */
@RestController
@RequestMapping("/doc")
public class MedicperRestController {


    @Resource
    private MedicperService medicperService;

    @PostMapping("/del")
    public String delWorkerAndMedicper(String[] woJubnum){
        Integer result = medicperService.delWorkerAndMedicper(woJubnum,woJubnum);
        String msg = (result>0?"操作成功!":"操作失败!");
        return msg;
    }



    @PostMapping( "/uploadPic")
    public String uploadPic(@RequestParam(value="myfile") MultipartFile myfile, HttpServletRequest req) throws IOException {
        //指定上传路径 获取要上传到的文件名，
        String realPath = req.getServletContext().getRealPath("/res/images");
        File folder = new File(realPath);
        //判断文件是否存在，不存在的话自动创建一个
        if(folder.exists()==false){
            folder.mkdirs();
        }

        String oldName = myfile.getOriginalFilename();//获取上传前的名字
        int index = oldName.lastIndexOf(".");//获取最后一个点
        String newName = UUID.randomUUID().toString().replace("-","")+oldName.substring(index);
        File newFile = new File(folder,newName);
        Thumbnails.of(myfile.getInputStream()).size(150, 150).toFile(newFile);
        String json = JSON.toJSONString(newName);
        return json;
    }



    @GetMapping("/search")
    public Pager search(Integer offset, Integer limit, MedicperDto medicperDto){
        if(medicperDto.getEndDate()!=null){
            Date dd = medicperDto.getEndDate();
            Calendar calendar = Calendar.getInstance();//获取日历实例
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date d = calendar.getTime();
            medicperDto.setEndDate(d);
        }
        Pager pager = new Pager();
        HttpSession session = SystemContext.getHtppSession();
        Worker worker = (Worker) session.getAttribute(Constants.LOGIN_USER);
        medicperDto.setRoleId(worker.getRoleId());
        medicperDto.setWoJubnum(worker.getWoJubnum());
        pager = medicperService.searchByMedicperByWhere(medicperDto,offset,limit);

        return  pager;
    }

    @PostMapping("/searchRole")
    public List<Role> searchRole(Integer deId){
        return medicperService.getRoleByWoJubnum(deId);
    }
    @PostMapping("/searchDivision")
    public List<Division> searchDivision(){
        return medicperService.getDivision();
    }

    @PostMapping("searchRoleByroleId")
    public Role getRole(Integer roleId){
        Role role = medicperService.getRoleByroleId(roleId);
        return role;
    }

}
